﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTH_DTO;
using QLTH_BUSINESS;
using unitlControl;

namespace QuanLy_THPT_S4
{
    public partial class frmGiaoVien : Form
    {
        private readonly clsGIaoVienBU _gvBU;
        private readonly clsGiaoVienDTO _gvDTO;
        private readonly clsDanTocBU _dtBU;
        private readonly clsTonGiaoBU _tgBU;
        private readonly clsHocViBU _hvBU;
        private readonly clsTCMBU  _tcmBU;

        public frmGiaoVien()
        {
            InitializeComponent();
            _gvBU = new clsGIaoVienBU();
            _gvDTO = new clsGiaoVienDTO();
            _dtBU = new clsDanTocBU();
            _tgBU = new clsTonGiaoBU();
            _hvBU = new clsHocViBU();
            _tcmBU = new clsTCMBU();
        }
        private void dieukhien(Boolean dk)
        {
            btnCapNhat.Enabled = dk;
            simpleButton1.Enabled = dk;
            btnXoa.Enabled = dk;
            btnThemExcel.Enabled = dk;
        }
   
        
        private void frmGiaoVien_Load(object sender, EventArgs e)
        {
           gridControl1.DataSource = _gvBU.DanhSachGiaoVien();
           progressBarControl1.EditValue = 0;
           progressBarControl1.Hide();
           dieukhien(true);
           
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            frmGiaoVienUPDATE frm = new frmGiaoVienUPDATE();
            DialogResult result = frm.ShowDialog();
            if (result == DialogResult.Cancel)
            {
                gridControl1.DataSource = _gvBU.DanhSachGiaoVien();
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frmGiaoVienUPDATE frm = new frmGiaoVienUPDATE();
            frm.Magv = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAGV").ToString();
            int RowHandle = gridView1.FocusedRowHandle;
            DialogResult result = frm.ShowDialog();
           
            if (result == DialogResult.Cancel)
            {
                if (result == DialogResult.Cancel)
                {
                  frmGiaoVien_Load(sender, e);
                  gridView1.FocusedRowHandle = RowHandle;
                }
            }
           
            
        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {
            if (textEdit1.Text == "" || textEdit1.Text == null)
            {
                gridControl1.DataSource = _gvBU.DanhSachGiaoVien();
            }
            else
            {
                gridControl1.DataSource = _gvBU.DanhSachGiaoVienDK(textEdit1.Text);
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {

            if (MessageboxUtils.YesNo("Bạn có muốn xóa giáo viên") == DialogResult.Yes)
            {
                _gvDTO.Magv = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "MAGV").ToString();
                _gvBU.Xoa_giaovien(_gvDTO);
                frmGiaoVien_Load(sender, e);
            }
            
        }

        private void btnThemExcel_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            int _record = 0;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String _pach = ofd.FileName.ToString();
                DataTable dsGV_Excel = new DataTable();
                _gvBU.mo_excel(_pach);
                dsGV_Excel = _gvBU.DanhSachGiaoVienExcel();
                dieukhien(false);
                progressBarControl1.Properties.ShowTitle = true;
                progressBarControl1.Properties.Step = 1;
                progressBarControl1.Properties.PercentView = false;
                progressBarControl1.Properties.Minimum = 0;
                progressBarControl1.Properties.Maximum = dsGV_Excel.Rows.Count;
                if (dsGV_Excel.Rows.Count > 1)
                {
                    for (int i = 0; i < dsGV_Excel.Rows.Count; i++)
                    {
                        try
                        {
                            _gvDTO.Magv = toancuc.Tao_ma(_gvBU.macuoi(), "GV", 3);
                            _gvDTO.Holot = dsGV_Excel.Rows[i]["HOLOT"].ToString().Trim();
                            _gvDTO.Ten = dsGV_Excel.Rows[i]["TEN"].ToString().Trim();
                            if (dsGV_Excel.Rows[i]["SDT"].ToString().Trim() == "")
                            {
                                _gvDTO.Sdt = null;
                            }
                            else
                            {
                                _gvDTO.Sdt = int.Parse(dsGV_Excel.Rows[i]["SDT"].ToString().Trim());
                            }
                            if (dsGV_Excel.Rows[i]["PHAI"].ToString().Trim() == "")
                            {

                            }
                            else
                            {
                                if (dsGV_Excel.Rows[i]["PHAI"].ToString().Trim() == "Nam")
                                {
                                    _gvDTO.Phai = 0;
                                }
                                else
                                {
                                    _gvDTO.Phai = 1;
                                }
                            }
                            _gvDTO.Diachi = dsGV_Excel.Rows[i]["DIACHI"].ToString().Trim();
                            _gvDTO.Noisinh = dsGV_Excel.Rows[i]["NOISINH"].ToString().Trim();
                            _gvDTO.Ngaysinh = toancuc.ddMMyyyy(dsGV_Excel.Rows[i]["NGAYSINH"].ToString().Trim());
                            _gvDTO.Hocvi.Mahv = _hvBU.Laymahv(dsGV_Excel.Rows[i]["HOCVI"].ToString().Trim());
                            _gvDTO.tongiao.Matg = _tgBU.Laymatg(dsGV_Excel.Rows[i]["TONGIAO"].ToString().Trim()).ToString();
                            _gvDTO.dantoc.Madt = _dtBU.Laymadt(dsGV_Excel.Rows[i]["DANTOC"].ToString().Trim()).ToString();
                            _gvDTO.Tcm.Matcm = _tcmBU.Laymatcm(dsGV_Excel.Rows[i]["TOCHUYENMON"].ToString().Trim()).ToString();
                            _gvDTO.Cmnd =Convert.ToInt64(dsGV_Excel.Rows[i]["CMND"].ToString().Trim());

                        }
                        catch
                        {
                        
                        }
                        

                        _record += _gvBU.Them_giaovien(_gvDTO);
                        progressBarControl1.Show();
                        progressBarControl1.PerformStep();
                        progressBarControl1.Update();
               
                    }
                    MessageboxUtils.Success("Thêm thành công " + _record + " Giáo viên");
                }   
                frmGiaoVien_Load(sender, e);
            }
        }

   

  

     }
}
